require("luaext")

local moveUp = "up"
local moveDown = "down"
local moveLeft = "left"
local moveRight = "right"

local Rule = class("Rule")

function Rule:ctor()
	
end

--格子是否有解
--返回解的坐标， 与动作方向
function Rule:isDie(maps, x, y) 
	local row = #maps[1]+1
	local col = #maps+1
	local lx1 = x - 1 > 0;
	local lx2 = x - 2 > 0;
	local lx3 = x - 3 > 0;
	local bx1 = x + 1 < row;
	local bx2 = x + 2 < row;
	local bx3 = x + 3 < row;
	local ly1 = y - 1 > 0;
	local ly2 = y - 2 > 0;
	local ly3 = y - 3 > 0;
	local by1 = y + 1 < col;
	local by2 = y + 2 < col;
	local by3 = y + 3 < col;
	local color = maps[y][x];
	if color==0 then 
		return true
	end
	local function isMapValue(x,y)
		if maps[y] and maps[y][x] and maps[y][x]>0 then
			return true
		end
		return false
	end	
	if (bx1) then
		
		if (maps[y][x + 1] == color) then --相同的在右边
			if (bx3) then
				if (maps[y][x + 3] == color) and isMapValue(x+2,y) then							
					return false, x+3, y, moveLeft
				end
			end
			if (bx2 and by1) then
				if (maps[y + 1][x + 2] == color) and isMapValue(x+2,y+2)  then					
					return false, x+2, y+1, moveUp
				end
			end
			if (bx2 and ly1) then
				if (maps[y - 1][x + 2] == color) and isMapValue(x+2,y-2)  then					
					return false, x+2, y-1, moveDown
				end
			end
			if (lx2) then
				if (maps[y][x - 2] == color) and isMapValue(x-1,y)  then					
					return false, x-2, y, moveRight
				end
			end
			if (lx1 and ly1) then
				if (maps[y - 1][x - 1] == color) and isMapValue(x-1,y-2)  then		
					return false, x-1, y-1, moveDown
				end
			end
			if (lx1 and by1) then
				if (maps[y + 1][x - 1] == color) and isMapValue(x-1,y)  then					
					return false, x-1, y+1, moveUp
				end
			end
		end
		if (ly1 and by1) then
			if (maps[y - 1][x + 1] == color and maps[y + 1][x + 1] == color) then				
				return false, x+1, y, moveLeft
			end
		end
	end
	if (lx1) then
		if (maps[y][x - 1] == color) then --相同的在左边
			if (lx3) then
				if (maps[y][x - 3] == color) and isMapValue(x-2,y) then
					return false, x-3, y, moveRight
				end
			end
			if (lx2 and by1) then
				if (maps[y + 1][x - 2] == color) and isMapValue(x-2,y)  then
					return false, x-2, y+1, moveUp
				end
			end
			if (lx2 and ly1) then
				if (maps[y - 1][x - 2] == color) and isMapValue(x-2,y)  then
					return false, x-2, y-1, moveDown
				end
			end
			if (bx2) then
				if (maps[y][x + 2] == color) and isMapValue(x+3,y)  then
					return false, x+2, y, moveRight
				end
			end
			if (bx1 and ly1) then
				if (maps[y - 1][x + 1] == color) and isMapValue(x+1,y)  then
					return false, x+1, y-1, moveDown
				end
			end
			if (bx1 and by1) then
				if (maps[y + 1][x + 1] == color) and isMapValue(x+1,y)  then
					return false, x+1, y+1, moveUp
				end
			end
		end
		if (ly1 and by1) then
			if (maps[y - 1][x - 1] == color and maps[y + 1][x - 1] == color) then
				return false, x-1, y, moveRight 
			end
		end
	end
	if (by1) then
		if (maps[y + 1][x] == color) then --相同的在下边
			if (by3) then
				if (maps[y + 3][x] == color) and isMapValue(x,y+2)  then
					return false, x, y+3, moveUp
				end
			end
			if (lx1 and by2) then
				if (maps[y + 2][x - 1] == color) and isMapValue(x,y+2)  then
					return false, x-1, y+2, moveRight
				end
			end
			if (bx1 and by2) then
				if (maps[y + 2][x + 1] == color) and isMapValue(x,y+2)  then
					return false, x+1, y+2, moveLeft
				end
			end
			if (ly2) then
				if (maps[y - 2][x] == color) and isMapValue(x,y-1)  then
					return false, x, y-2, moveUp
				end
			end
			if (bx1 and ly1) then
				if (maps[y - 1][x + 1] == color) and isMapValue(x,y-1)  then
					return false, x+1, y-1, moveLeft
				end
			end
			if (lx1 and ly1) then
				if (maps[y - 1][x - 1] == color) and isMapValue(x,y-1)  then
					return false, x-1, y-1, moveRight
				end
			end
		end
		if (lx1 and bx1) then --在第一行
			if (maps[y + 1][x - 1] == color and maps[y + 1][x + 1] == color) then
				return false, x, y+1, moveUp
			end
		end
	end
	if (ly1) then
		if (maps[y - 1][x] == color) then --相同的在上边
			if (ly3) then
				if (maps[y - 3][x] == color) and isMapValue(x,y-2)  then
					return false, x, y-3, moveDown
				end
			end
			if (lx1 and ly2) then
				if (maps[y - 2][x - 1] == color) and isMapValue(x,y-2)  then
					return false, x-1, y-2, moveRight
				end
			end
			if (bx1 and ly2) then
				if (maps[y - 2][x + 1] == color) and isMapValue(x,y-2)  then
					return false, x+1, y-2, moveLeft
				end
			end
			if (by2) then
				if (maps[y + 2][x] == color) and isMapValue(x,y+1)  then
					return false, x, y+2, moveUp
				end
			end
			if (bx1 and by1) then
				if (maps[y + 1][x + 1] == color) and isMapValue(x,y+1)  then
					return false, x+1, y+1, moveLeft
				end
			end
			if (lx1 and by1) then
				if (maps[y + 1][x - 1] == color) and isMapValue(x,y+1)  then
					return false, x-1, y+1, moveRight
				end
			end
		end
		if (lx1 and bx1) then --在底行
			if (maps[y - 1][x - 1] == color and maps[y - 1][x + 1] == color) then
				return false, x, y-1, moveDown
			end
		end
	end
	return true;
end

--测试
local maps = {
	{4,2,3,0,3,4,0},
	{1,3,5,2,1,2,2},
	{0,3,0,0,0,0,0},
	{0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0},
}
local rule = Rule.new()
local str,x, y, action = rule:isDie(maps, 3, 1)

print(str, x, y, action)

return Rule